<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="城市名称" prop="cityName">
        <el-input
          v-model="queryParams.cityName"
          placeholder="请输入城市名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:talent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:talent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:talent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:talent:add']"
          icon="el-icon-upload2"
          size="mini"
          type="info"
          @click="handleImport"
        >导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:talent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="talentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号"  align="center" type="index"/>
      <el-table-column label="装配式制造" align="center" prop="assembly" />
      <el-table-column label="施工人才" align="center" prop="construction" />
      <el-table-column label="物流人才" align="center" prop="logistics" />
      <el-table-column label="建筑服务业人才" align="center" prop="service" />
      <el-table-column label="原材料" align="center" prop="materials" />
      <el-table-column label="城市名称" align="center" prop="cityName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:talent:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:talent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="importUpload.title"
      :visible.sync="importUpload.open"
      append-to-body
      width="400px"
    >
      <el-upload
        ref="upload"
        :action="importUpload.url"
        :auto-upload="false"
        :disabled="importUpload.isUploading"
        :headers="importUpload.headers"
        :limit="1"
        :on-progress="handleImportFileUploadProgress"
        :on-success="handleImportFileSuccess"
        accept=".xlsx, .xls"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip">
          <el-button size="mini" type="primary" @click="importTemplate">
            下载模板<i class="el-icon-download el-icon&#45;&#45;right"></i>
          </el-button>
          <div style="color: red; display: inline">
            提示：仅允许导入“xls”或“xlsx”格式文件！
          </div>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="importUpload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改人才总览对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="装配式制造" prop="assembly">
          <el-input v-model="form.assembly" placeholder="请输入装配式制造" />
        </el-form-item>
        <el-form-item label="施工人才" prop="construction">
          <el-input v-model="form.construction" placeholder="请输入施工人才" />
        </el-form-item>
        <el-form-item label="物流人才" prop="logistics">
          <el-input v-model="form.logistics" placeholder="请输入物流人才" />
        </el-form-item>
        <el-form-item label="建筑服务业人才" prop="service">
          <el-input v-model="form.service" placeholder="请输入建筑服务业人才" />
        </el-form-item>
        <el-form-item label="原材料" prop="materials">
          <el-input v-model="form.materials" placeholder="请输入原材料" />
        </el-form-item>
        <el-form-item label="城市id" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTalent, getTalent, delTalent, addTalent, updateTalent } from "@/api/system/talent";
import { getToken } from '@/utils/auth'

export default {
  name: "Talent",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人才总览表格数据
      talentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assembly: null,
        construction: null,
        logistics: null,
        service: null,
        materials: null,
        cityCode: null,
      },
      importUpload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/talent/importData",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assembly: [
          { required: true, message: "装配式制造不能为空", trigger: "blur" }
        ],
        construction: [
          { required: true, message: "施工人才不能为空", trigger: "blur" }
        ],
        logistics: [
          { required: true, message: "物流人才不能为空", trigger: "blur" }
        ],
        service: [
          { required: true, message: "建筑服务业人才不能为空", trigger: "blur" }
        ],
        materials: [
          { required: true, message: "原材料不能为空", trigger: "blur" }
        ],
        cityCode: [
          { required: true, message: "城市id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openLoading() {
      this.loading_const = this.$loading({
        lock: true,
        text: "导入时间较长,请耐心等待。",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        customClass: "loading_area",
      });
    },
    // 提交上传文件
    submitFileForm() {
      this.openLoading();
      this.$refs.upload.submit();
    },
    importTemplate(){
      const  fileName = "人才总揽20220630.xls"
      this.download('system/marks/importTemplate', {
        fileName
      }, fileName)
    },
    //导入人才总览
    handleImport() {
      this.importUpload.title = "批量导入人才总览";
      this.importUpload.open = true;
    },
    // 文件上传中处理
    handleImportFileUploadProgress(event, file, fileList) {
      this.$refs.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleImportFileSuccess(response, file, fileList) {
      this.importUpload.open = false;
      this.importUpload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.loading_const.close();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 查询人才总览列表 */
    getList() {
      this.loading = true;
      listTalent(this.queryParams).then(response => {
        this.talentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assembly: null,
        construction: null,
        logistics: null,
        service: null,
        materials: null,
        cityCode: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人才总览";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTalent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人才总览";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTalent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTalent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人才总览编号为"' + ids + '"的数据项？').then(function() {
        return delTalent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/talent/export', {
        ...this.queryParams
      }, `talent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
